package com.project.controller;

import com.project.bean.BuyCarBean;
import com.project.bean.UserBean;
import com.project.service.IBuyCarService;
import com.project.util.QiniuCloudUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("buyCar")
public class BuyCarController {
    @Autowired
    private IBuyCarService buyCarService;
    @GetMapping("add")
    public String add(BuyCarBean buyCar,HttpServletRequest request) {
        UserBean userBean= (UserBean) request.getSession().getAttribute("loginUser");
        buyCar.setUserNum(userBean.getUserNum());
        buyCarService.add(buyCar);
        return "ok";
    }
    @GetMapping("delBuyCar")
    public String delBuyCar(int buyCarId) {
        buyCarService.delBuyCar(buyCarId);
        return "ok";
    }
    @GetMapping("updateNum")
    public String updateNum(int buyCarId, int newNum) {
       return buyCarService.updateNum(buyCarId, newNum);
    }
    @GetMapping("findUserBuyCar")
    public List<BuyCarBean> findUserBuyCar(HttpServletRequest request) {
        UserBean userBean= (UserBean) request.getSession().getAttribute("loginUser");
        List<BuyCarBean> buyCarBeans= buyCarService.findUserBuyCar(userBean.getUserNum());
        buyCarBeans.forEach(n->{
            String img= (String) QiniuCloudUtil.QiniuCloudDownloadImage(n.getProductImg());
            n.setProductImg(img);
        });
      return  buyCarBeans;
    }
}
